import _ from 'lodash'
import dayjs from 'dayjs'
import * as dayjs from 'dayjs'
import advancedFormat from 'dayjs/plugin/advancedFormat'
import weekday from 'dayjs/plugin/weekday'
import weekOfYear from 'dayjs/plugin/weekOfYear'
import weekYear from 'dayjs/plugin/weekYear'
dayjs.extend(advancedFormat)
dayjs.extend(weekday)
dayjs.extend(weekOfYear)
dayjs.extend(weekYear)

export default {
  // 后一天
  getAfterDate(date = new Date()) {
    return dayjs(date).add(1, 'day').format('YYYY-MM-DD')
  },
  // 前一天
  getBeforeDate(date = new Date()) {
    return dayjs(date).subtract(1, 'day').format('YYYY-MM-DD')
  },
  // 当天
  getCurrentDate(data = new Date(), format = 'YYYY-MM-DD') {
    return dayjs(data).format(format)
  },

  // 计算日期
  computeDate(date: any = new Date(), num = 1, type = 'add') {
    if (type === 'add') {
      return dayjs(date).add(num, 'day').format('YYYY-MM-DD')
    } else {
      return dayjs(date).subtract(num, 'day').format('YYYY-MM-DD')
    }
  },

  // 前一周
  getBeforeWeekDate(num: number = 1) {
    const weekNum = dayjs(this.getCurrentDate()).week()
    const week = dayjs(this.getCurrentDate())
      .week(weekNum - num)
      .format('YYYY-MM-DD')
    let day = dayjs(week).day()

    if (day === 0) {
      day = 7
    }

    const range = []

    range.push({
      num: day,
      date: week
    })

    let m = 1
    for (let i = day - 1; i <= day && i > 0; i--) {
      const d = this.computeDate(week, m, 'subtract')

      range.push({
        num: i,
        date: d
      })
      m += 1
    }

    let n = 1
    for (let i = day + 1; i < 8; i++) {
      const d = this.computeDate(week, n, 'add')
      range.push({
        num: i,
        date: d
      })
      n += 1
    }

    return _.orderBy(range, ['num'])
  },
  // 月开始日期
  getMonthStartDate(date = new Date(), format = 'YYYY-MM-DD') {
    return dayjs(date).startOf('month').format(format)
  },
  // 月结束日期
  getMonthEndDate(date = new Date(), format = 'YYYY-MM-DD') {
    return dayjs(date).endOf('month').format(format)
  },
  // 当前时间戳
  getTimeStamp(date: any = new Date()) {
    return dayjs(date).unix()
  },
  // 当前星期
  getWeek() {
    let week = ''
    switch (dayjs().day()) {
      case 0:
        week = '日'
        break
      case 1:
        week = '一'
        break
      case 2:
        week = '二'
        break
      case 3:
        week = '三'
        break
      case 4:
        week = '四'
        break
      case 5:
        week = '五'
        break
      case 6:
        week = '六'
        break
      default:
        break
    }
    return '星期' + week
  },
  // 格式化时间戳
  formatTimeStamp(timeStamp = 0, format = 'YYYY-MM-DD HH:mm:ss') {
    if (timeStamp === 0) {
      timeStamp = this.getTimeStamp()
    }

    return dayjs.unix(timeStamp).format(format)
  },
  // 格式化时间戳-毫秒级
  formatTimeStamp_2(timeStamp = dayjs(new Date()), format = 'YYYY-MM-DD HH:mm:ss') {
    return dayjs(timeStamp).format(format)
  },

  getRange(startDate: any, endDate: any) {
    const fromDate = dayjs(startDate)
    const toDate = dayjs(endDate)
    const diff = toDate.diff(fromDate, 'day')
    const range = []
    for (let i = 0; i <= diff; i++) {
      range.push(dayjs(startDate).add(i, 'day').format('YYYY-MM-DD'))
    }
    return range
  },

  // 求间隔时间数
  interval(startTime: any, endTime: any, interval: any) {
    // 获取当前日期
    const currentDate = new Date()
    // 构建开始时间和结束时间的完整日期字符串
    const startDateString = `${currentDate.toISOString().slice(0, 10)} ${startTime}`
    const endDateString = `${currentDate.toISOString().slice(0, 10)} ${endTime}`

    // 将开始时间和结束时间转换为Date对象
    const startDate = new Date(startDateString) as any
    const endDate = new Date(endDateString)

    // 计算时间点
    const timePoints = []
    const currentTime = startDate

    // eslint-disable-next-line no-unmodified-loop-condition
    while (currentTime <= endDate) {
      const hours = currentTime.getHours().toString().padStart(2, '0')
      const minutes = currentTime.getMinutes().toString().padStart(2, '0')
      const seconds = currentTime.getSeconds().toString().padStart(2, '0')

      timePoints.push(`${hours}:${minutes}:${seconds}`)

      currentTime.setSeconds(currentTime.getSeconds() + interval)
    }
    return timePoints.length
  }
}
